using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Business;
using System.Reflection;
public partial class UserControl_ProductTree : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["addnew"] != null)
        {
            String type = Request.QueryString["addnew"];
            String[] para ={ "-1"};
            SwichPage(type, para);

           // Response.Write("<Script>window.location.querystring['addnew']='ada'</Script>");
            if (!IsPostBack)
                LoadTree();

            return;
        }

        if (!IsPostBack)
        {

            LoadTree();
           // Response.Write("khong postback");
            Response.Write(tvProduct.SelectedValue);
            if (Request.QueryString["selectValue"] != null)
            {
                //Response.Write(Request.QueryString["selectValue"]);
                TreeNode tn = tvProduct.FindNode(Request.QueryString["selectValue"]);
                tn.Select();
                if (tn.Depth == 1)
                    tn.Parent.Expand();
                else if (tn.Depth == 2)
                {
                    tn.Parent.Expand();
                    tn.Parent.Parent.Expand();
                }
                String[] str = tvProduct.SelectedValue.Split('.');
                String type = str[0];
                String[] para ={ str[1] };
                SwichPage(type, para);
                return;
            }
        }
        if (tvProduct.SelectedValue != "")
        {
            //Response.Write("the anh"+tvProduct.SelectedValue);
            String[] str = tvProduct.SelectedValue.Split('.');
            String type = str[0];
            String[] para ={ str[1] };
            SwichPage(type, para);
        }

        //if(tvProduct.SelectedNode!=null)
        // Response.Write(tvProduct.SelectedNode.ValuePath);

    }

    private void SwichPage(String type, String[] para)
    {
        UserControl uc;
        if (type == "Product")
        {
            uc = MyLoadControl("~/UserControl/ProductManager.ascx", para);
            uc.ID = tvProduct.SelectedValue;
            //Panel1.Controls.Clear();
            Panel1.Controls.Add(uc);

        }
        else if (type == "Plan")
        {
            uc = MyLoadControl("~/UserControl/PlanManager.ascx", para);
            uc.ID = tvProduct.SelectedValue;
            //Panel1.Controls.Clear();
            Panel1.Controls.Add(uc);
        }
        else if (type == "PlanDetail")
        {
            uc = MyLoadControl("~/UserControl/PlanDetailManager.ascx", para);
            uc.ID = tvProduct.SelectedValue;
            //Panel1.Controls.Clear();
            Panel1.Controls.Add(uc);
        }
    }

    private void LoadTree()
    {
        DataTable dt_Product = bProduct.getAllProduct();
        DataTable dt_Plan = bPlan.getAllPlan();
        DataTable dt_PlanDetail = bPlanDetail.getAllPlanDetail();
        bool first = true;
        foreach (DataRow dr_Product in dt_Product.Rows)
        {
            TreeNode tn_product = new TreeNode(dr_Product["producName"].ToString(), "Product." + dr_Product["productID"].ToString());
            foreach (DataRow dr_Plan in dt_Plan.Rows)
            {
                if ((int)dr_Plan["productID"] == (int)dr_Product["productID"])
                {
                    TreeNode tn_Plan = new TreeNode(dr_Plan["planName"].ToString(), "Plan." + dr_Plan["planID"].ToString());
                    foreach (DataRow dr_PlanDetail in dt_PlanDetail.Rows)
                    {
                        if ((int)dr_PlanDetail["planID"] == (int)dr_Plan["planID"])
                        {
                            TreeNode tn_PlanDetail = new TreeNode(dr_PlanDetail["PlanDetailName"].ToString(), "PlanDetail." + dr_PlanDetail["PlanDetailID"].ToString());
                            tn_Plan.ChildNodes.Add(tn_PlanDetail);
                        }
                    }
                    tn_product.ChildNodes.Add(tn_Plan);

                }
            }
            if (first)
            {
                tn_product.Expand();
               // tn_product.Select();
                first = false;
            }
            else
            {
                tn_product.Collapse();
            }
            tvProduct.Nodes.Add(tn_product);
        }
    }

    protected void tvProduct_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (Request.QueryString["addnew"] != null)
            Response.Redirect("ProductManage.aspx?selectValue=" + tvProduct.SelectedNode.ValuePath);

        //String[] str = tvProduct.SelectedValue.Split('.');
        //String type = str[0];
        //String[] para ={ str[1] };
        //if (type == "Product")
        //{
        //    UserControl uc = MyLoadControl("UserControl/ProductManager.ascx", para);
        //    Panel1.Controls.Clear();
        //    Panel1.Controls.Add(uc);

        //}
    }

    private UserControl MyLoadControl(string UserControlPath, params object[] constructorParameters)
    {
        List<Type> constParamTypes = new List<Type>();
        foreach (object constParam in constructorParameters)
        {
            constParamTypes.Add(constParam.GetType());
        }

        UserControl ctl = Page.LoadControl(UserControlPath) as UserControl;

        // Find the relevant constructor
        ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray());

        //And then call the relevant constructor
        if (constructor == null)
        {
            throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString());
        }
        else
        {
            constructor.Invoke(ctl, constructorParameters);
        }

        // Finally return the fully initialized UC
        return ctl;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}
